﻿using MediatR;
using Service.Auth.Domain.AggregatesModel;
using Service.Core.Repository;
using Service.Core.Repository.Attribute;
using Service.Framework;
using Service.Framework.Utils;
using Service.Auth.Api.Application.Command;

namespace Service.Auth.Api.Application.Handler.CommandHandler;

// Regular CommandHandler
/// <summary>
/// 
/// </summary>
/// <param name="permissionRepository"></param>
/// <param name="moduleRepository"></param>
public class PermissionsCommandHandler(
    IRepository<Permission> permissionRepository,
    IRepository<Module> moduleRepository)
    :
        IRequestHandler<CreatePermissionsCommand, bool>,
        IRequestHandler<UpdatePermissionsCommand, bool>,
        IRequestHandler<DeletePermissionsCommand, bool>
{
    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    [UnitOfWork]
    public async Task<bool> Handle(CreatePermissionsCommand request, CancellationToken cancellationToken)
    {
        var permissionDto = request.Permissions;
        var permission = new Permission(permissionDto.Code, permissionDto.Name, permissionDto.IsButton, permissionDto.IsHide, permissionDto.Func,
            permissionDto.OrderSort, permissionDto.Icon, permissionDto.Description, permissionDto.Component);
        await CheckPermissions(permission, permissionDto.ModuleUrl, permissionDto.ParentId);
        return await permissionRepository.InsertAsync(permission, cancellationToken);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    /// <exception cref="LogicErrorException"></exception>
    [UnitOfWork]
    public async Task<bool> Handle(DeletePermissionsCommand request, CancellationToken cancellationToken)
    {
        var permission = await permissionRepository.GetByIdAsync(request.Permission.Id, cancellationToken);
        if (permission == null)
            throw new LogicErrorException("删除失败，菜单权限不存在。");
        return await permissionRepository.DeleteAsync(permission, cancellationToken);
    }

    /// <summary>
    /// 
    /// </summary>
    /// <param name="request"></param>
    /// <param name="cancellationToken"></param>
    /// <returns></returns>
    /// <exception cref="LogicErrorException"></exception>
    [UnitOfWork]
    public async Task<bool> Handle(UpdatePermissionsCommand request, CancellationToken cancellationToken)
    {
        var permissionDto = request.Permissions;
        var permission = await permissionRepository.GetByIdAsync(permissionDto.Id, cancellationToken);
        if (permission == null)
            throw new LogicErrorException("修改失败，菜单权限不存在。");

      

        permission.Code = permissionDto.Code;
        permission.Name = permissionDto.Name;
        permission.IsButton = permissionDto.IsButton;
        permission.IsHide = permissionDto.IsHide;
        permission.Func = permissionDto.Func;
        permission.OrderSort = permissionDto.OrderSort;
        permission.Icon = permissionDto.Icon;
        permission.Component = permissionDto.Component;
        permission.Description = permissionDto.Description;
        await CheckPermissions(permission, permissionDto.ModuleUrl, permissionDto.ParentId);
        return await permissionRepository.UpdateAsync(permission, cancellationToken);
    }



    private async Task CheckPermissions(Permission permission, string moduleUrl, long? parentId)
    {
        permission.SetParenId(parentId.HasValue ? parentId.Value : 0);
        if (permission.IsButton)
        {
            if (moduleUrl.IsNullOrWhiteSpace())
            {
                throw new LogicErrorException("接口地址不能为空。");
            }
        }
        permission.SetModuleUrl(moduleUrl);
    }




}